using System;
using System.Collections;
using System.Xml;

namespace Scenario.Modules.PCM.DataModel.ConfigurationManager.Rule
{
	//*********************************************************************
	//
	// XMLRuless Class
	//
	// The XMLRuless class represents a collection 
	// base of XMLRules
	// Its imports methods from WAPI.XmlObjectSet like get set and add
	//
	//*********************************************************************
	public class XMLRules : bOS.XML.Data.XMLObjectSet 
	{
		public const string MAIN_NODE = "Rules";

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Empty **/
		public XMLRules()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLRules(System.Xml.XmlNode nodeList)
		{
			this.Parse(nodeList);
		}

		/** PROPERTIES **/

		/** INSTANCE METHODS **/

		override public void Init()
		{
			this.RemoveAll();
		}

		public new XMLRulesFamily this[ int index ]  
		{
			get  
			{
				return( (XMLRulesFamily) List[index] );
			}
			set  
			{
				List[index] = value;
			}
		}

		override public void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<nodeList.ChildNodes.Count;i++)
			{
				node = nodeList.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLRulesFamily.MAIN_NODE:
						this.Add ( new XMLRulesFamily(node));
						break;
				}
			}
		}

		/** Method for return a rule from collection **/
		public XMLRulesFamily Get(string sName)
		{
			XMLRulesFamily rule;

			for ( int i=0; i<this.List.Count; i++)
			{
				rule = (XMLRulesFamily)this.List[i];
				if ( rule.sName == sName )
				{
					return rule;
				}
			}

			throw new Exception.RulesFamilyNotFound(sName + " Not Found.");
		}

		override public bOS.XML.XMLNode GetXMLNode()
		{
			bOS.XML.XMLNode xmlNode = new bOS.XML.XMLNode(XMLRules.MAIN_NODE);

			for ( int i=0; i<this.Count;i++)
			{
				XMLRulesFamily family = (XMLRulesFamily)this.List[i];
				xmlNode.AddNode ( family.GetXMLNode() );
			}

			return xmlNode;
		}

		
		/** WAPI Wrappers to call SCENARIO ENGINE **/

		/** no wapi **/

		/** SORTING  FIELDS **/

		public enum SortFields 
		{
			Name,
			Description
		}

		/** SORTING METHODS **/


		public void Sort(SortFields sortField, bool isAscending) 
		{
			switch (sortField) 
			{
				case SortFields.Name:
					InnerList.Sort(new NameComparer());
					break;
				case SortFields.Description:
					InnerList.Sort(new DescriptionComparer());
					break;
				
			}
			if (!isAscending) InnerList.Reverse();
		}


		private sealed class NameComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLRulesFamily first = (XMLRulesFamily) x;
				XMLRulesFamily second = (XMLRulesFamily) y;
				return first.Name.CompareTo(second.Name);
			}
		}

		private sealed class DescriptionComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLRulesFamily first = (XMLRulesFamily) x;
				XMLRulesFamily second = (XMLRulesFamily) y;
				return first.Description.CompareTo(second.Description);
			}
		}

	}
}